<?php

add_theme_support('menus');
add_theme_support('post-thumbnails');

/* CUSTOM SIDEBARS */

if ( function_exists('register_sidebar') )
  register_sidebar(array(
    'name' => 'Contact Widgets',
    'before_widget' => '<div class = "widgetizedArea">',
    'after_widget' => '</div>',
    'before_title' => '<h3>',
    'after_title' => '</h3>',
  )
);

if ( function_exists('register_sidebar') )
  register_sidebar(array(
    'name' => 'Sidebar',
    'before_widget' => '<div class = "widgetizedArea">',
    'after_widget' => '</div>',
    'before_title' => '<h3>',
    'after_title' => '</h3>',
  )
);
if ( function_exists('register_sidebar') )
  register_sidebar(array(
    'name' => 'Blog Sidebar',
    'before_widget' => '<div class = "widgetizedArea">',
    'after_widget' => '</div>',
    'before_title' => '<h3>',
    'after_title' => '</h3>',
  )
);

if ( function_exists('register_sidebar') )
  register_sidebar(array(
    'name' => 'Footer Widget 1',
    'before_widget' => '<div class = "widgetizedArea">',
    'after_widget' => '</div>',
    'before_title' => '<h3>',
    'after_title' => '</h3>',
  )
);

if ( function_exists('register_sidebar') )
  register_sidebar(array(
    'name' => 'Footer Widget 2',
    'before_widget' => '<div class = "widgetizedArea">',
    'after_widget' => '</div>',
    'before_title' => '<h3>',
    'after_title' => '</h3>',
  )
);


if ( function_exists('register_sidebar') )
  register_sidebar(array(
    'name' => 'Footer Widget 3',
    'before_widget' => '<div class = "widgetizedArea">',
    'after_widget' => '</div>',
    'before_title' => '<h3>',
    'after_title' => '</h3>',
  )
);


if ( function_exists('register_sidebar') )
  register_sidebar(array(
    'name' => 'Footer Widget 4',
    'before_widget' => '<div class = "widgetizedArea">',
    'after_widget' => '</div>',
    'before_title' => '<h3>',
    'after_title' => '</h3>',
  )
);


/* CUSTOM SIDEBARS END */


/* TEXT WIDGET ALLOWS PHP */

add_filter('widget_text','execute_php',100);
function execute_php($html){
     if(strpos($html,"<"."?php")!==false){
          ob_start();
          eval("?".">".$html);
          $html=ob_get_contents();
          ob_end_clean();
     }
     return $html;
}

/* TEXT WIDGET ALLOWS PHP END */

/* CUSTOM POST TYPE */

add_action( 'init', 'create_post_type' );
function create_post_type() {
  register_post_type( 'services',
    array(
      'labels' => array(
        'name' => __( 'Services' ),
        'singular_name' => __( 'Services' )
      ),
      'public' => true,
      'has_archive' => true,
      'supports' => array('title', 'editor', 'thumbnail'),
      'rewrite' => array('slug' => 'services')
    )
  );
  register_post_type( 'key-sectors',
    array(
      'labels' => array(
        'name' => __( 'Key Sectors' ),
        'singular_name' => __( 'Key Sectors' )
      ),
      'public' => true,
      'has_archive' => true,
       'supports' => array('title', 'editor', 'thumbnail'),
      'rewrite' => array('slug' => 'key-sectors'),
    )
  );

}

/* CUSTOM POST TYPE END */

/* CUSTOM EXCERPT */

function print_excerpt($length) {
  global $post;
  $text = $post->post_excerpt;
  if ( '' == $text ) {
    $text = get_the_content('');
    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]>', $text);
  }
  $text = strip_shortcodes($text);
  $text = strip_tags($text);

  $text = substr($text,0,$length);
  $excerpt = reverse_strrchr($text, '.', 1);
  if( $excerpt ) {
    echo apply_filters('the_excerpt',$excerpt);
  } else {
    echo apply_filters('the_excerpt',$text);
  }
}

function reverse_strrchr($haystack, $needle, $trail) {
    return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) + $trail) : false;
}

/* CUSTOM EXCERPT END */

/* SHOW IMAGE CAPTION */

function the_post_thumbnail_caption() {
  global $post;

  $thumbnail_id    = get_post_thumbnail_id($post->ID);
  $thumbnail_image = get_posts(array('p' => $thumbnail_id, 'post_type' => 'attachment'));

  if ($thumbnail_image && isset($thumbnail_image[0])) {
    echo '<span>'.$thumbnail_image[0]->post_excerpt.'</span>';
  }
}

/* SHOW IMAGE CAPTION END */